package docsync;

import java.io.File;

public class AndroidFolder implements FolderInterface {
	File _dir;

	public AndroidFolder(File dir) {
		_dir = dir;
	}
	
	@Override
	public String toString() {
		return _dir.getAbsolutePath();
	}

	@Override
	public FileInterface create(String name) throws Exception {
		File f = new File(absolutePathOf(name));
		if (!f.createNewFile())
			throw new Exception("Fail to create new file: " + name);
		return new AndroidFile(f);
	}

	@Override
	public FileInterface[] getFiles() {
		File[] files = _dir.listFiles();
		FileInterface[] out = new FileInterface[files.length];
		for (int i = 0; i < files.length; ++i) {
			out[i] = new AndroidFile(files[i]);
		}
		return out;
	}

	@Override
	public FileInterface open(String name) {
		return new AndroidFile(absolutePathOf(name));
	}

	@Override
	public void remove(String name) throws Exception {
		if (new File(absolutePathOf(name)).delete() == false) {
			throw new Exception("Fail to remove file: " + name);
		}
	}

	private String absolutePathOf(String name) {
		return _dir.getAbsolutePath() + File.separator + name;
	}

}